// Copyright (c) 2010 Vanderlande Industries
// All rights reserved.
//
// The copyright to the computer program(s) herein is the property of 
// Vanderlande Industries. The program(s) may be used and/or copied
// only with the written permission of the owner or in accordance with
// the terms and conditions stipulated in the contract under which the
// program(s) have been supplied.

using System;
using System.Collections.Generic;
using System.Linq;
//using VI.Bfw.Logging;
using VI.Fsc.OperatorGui.Controllers;
using VI.Fsc.OperatorGui.Models.Application.Util;
using VI.Fsc.OperatorGui.Models.Fsc.Components;

namespace VI.Fsc.OperatorGui.Models.Fsc.Configuration
{
    public class CStructureItem
    {
        private string id;
        private string parentId;
        private string value;
        private DateTime timestamp;
        private string rawSectionId;
        private string rawZoneId;
        private string rawAreaId;


  

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        public string ExtendedId { get; set; }
       

        public string ParentId
        {
            get { return parentId; }
            set { parentId = value; }
        }

   

        public string RawSectionId
        {
            get { return rawSectionId; }
            set { rawSectionId = value; }
        }
        public string RawZoneId
        {
            get { return rawZoneId; }
            set { rawZoneId = value; }
        }
        public string RawAreaId
        {
            get { return rawAreaId; }
            set { rawAreaId = value; }
        }

        
        public string Value
        {
            get
            {
                return value;
            }
            set
            {
                this.value = value;
            }
        }

        public ComponentStatus Status
        {
            get
            {
                ComponentStatus status = ComponentStatus.OK;
                string rStateId = Id + "/" + CFscStrings.RSTATE_STRING;
                
                if (CFscConfiguration.Instance.StructureTree.ContainsKey(rStateId))
                {
                    string rStateString = CFscConfiguration.Instance.StructureTree[rStateId].Value.ToUpper();
                    
                    if (rStateString == "ERROR")
                    {
                        status = ComponentStatus.ERROR;
                    }
                    else if (rStateString == "WARNING")
                    {
                        status = ComponentStatus.WARNING;
                    }
                }
                
                return status;
            }
        }

        public string ProblemKey
        {
            get
            {
                Dictionary<string, object> row = new Dictionary<string, object>();
                string rsDetailsId = Id + "/" + CFscStrings.RSDETAILS_STRING;
                string rsDetails = "";
                
                if (CFscConfiguration.Instance.StructureTree.ContainsKey(rsDetailsId))
                {
                    rsDetails = CFscConfiguration.Instance.StructureTree[rsDetailsId].Value;
                }
                return rsDetails;
            }
        }

        public DateTime ErrorTime
        {
            get
            {
                string rsDetailsId = Id + "/" + CFscStrings.RSDETAILS_STRING;
                DateTime errorTime = DateTime.Now;

                if (CFscConfiguration.Instance.StructureTree.ContainsKey(rsDetailsId))
                {
                    errorTime = CFscConfiguration.Instance.StructureTree[rsDetailsId].Timestamp;
                }

                return errorTime;
            }
        }

        public DateTime Timestamp
        {
            get { return timestamp; }
            set { timestamp = value; }
        }

        

        public CStructureItem(string id, string parentId, object value)
        {
            Id = id;
            ParentId = parentId;

            string[] strings = id.Split('/').Last().Split('-');
            Type = strings.First();
            ExtendedId = strings.Last();
            
            string temp = id.Substring(id.LastIndexOf('/')+ 1);
            temp = temp.Substring(temp.IndexOf('-') + 1);

            if (!String.IsNullOrEmpty(temp))
            {
                if (temp.IndexOf('.') > 0)
                {
                    RawAreaId = temp.Substring(0, temp.IndexOf('.'));
                    temp = temp.Substring(temp.IndexOf('.') + 1);

                    if (temp.IndexOf('.') > 0)
                    {
                        RawZoneId = temp.Substring(0, temp.IndexOf('.'));
                        temp = temp.Substring(temp.IndexOf('.') + 1);

                        if (temp.IndexOf('.') > 0)
                        {
                            RawSectionId = temp.Substring(0, temp.IndexOf('.'));
                        }
                    }
                }
            }
            
            if(id.EndsWith("/" + CFscStrings.INNTEGRATIONLEVEL_STRING) || 
               id.EndsWith("/" + CFscStrings.MARKCODE_STRING) || 
               id.EndsWith("/" + CFscStrings.FUNCTIONVARIANT_STRING))
            {
                this.Value = value.ToString();
            }
        }

        public string DisplayName
        {
            get
            {
                    string parentName = this.Id;

                    if (parentName.LastIndexOf("/") > -1)
                    {
                        parentName = parentName.Substring(parentName.LastIndexOf("/") + 1);
                    }

                    // Given something like DVZ-1.3, we need to display "[DVZ-1.3] Divert zone"
                    if (parentName.LastIndexOf("-") > -1)
                    {
                        parentName = CUtility.GetFullComponentNameDisplayString(
                            parentName.Substring(parentName.LastIndexOf("-") + 1),
                            parentName.Substring(0, parentName.LastIndexOf("-")));
                    }
                    return parentName;
            }
        }

        public string MarkCode
        {
            get
            {
                string markCode =
                    Id + "/" +
                    CFscStrings.INFO_STRING + "/" +
                    CFscStrings.MARKCODE_STRING;

                if (CFscConfiguration.Instance.StructureTree.ContainsKey(markCode))
                {
                    markCode = CFscConfiguration.Instance.StructureTree[markCode].Value;
                }
                else
                {
                    markCode = Id;
                    // This else is for backwarse compatibility.
                    if (markCode.LastIndexOf("/") > -1)
                    {
                        markCode = markCode.Substring(markCode.LastIndexOf("/") + 1);
                    }

                    // Given something like DVZ-1.3, we need to display "[DVZ-1.3] Divert zone"
                    if (markCode.LastIndexOf("-") > -1)
                    {
                        markCode = markCode.Substring(0, markCode.LastIndexOf("-"));
                    }
                }
                return markCode;
            }
        }

        public string IntegrationLevel
        {
            get
            {
                string integrationLevel = string.Empty;
                string integrationLevelPath =
                    Id + "/" +
                    CFscStrings.INFO_STRING + "/" +
                    CFscStrings.INNTEGRATIONLEVEL_STRING;

                if (CFscConfiguration.Instance.StructureTree.ContainsKey(integrationLevelPath))
                {
                    integrationLevel = CFscConfiguration.Instance.StructureTree[integrationLevelPath].Value;
                }
                return integrationLevel;
            }
        }

        public string FunctionVariant
        {
            get
            {
                string functionVariant = string.Empty;
                string functionVariantPath =
                    Id + "/" +
                    CFscStrings.INFO_STRING + "/" +
                    CFscStrings.FUNCTIONVARIANT_STRING;

                if (CFscConfiguration.Instance.StructureTree.ContainsKey(functionVariantPath))
                {
                    functionVariant = CFscConfiguration.Instance.StructureTree[functionVariantPath].Value;
                }
                return functionVariant;
            }
        }

        private string ParentName
        {
            get
            {
                string parentName = this.Id;

                if (parentName.LastIndexOf("/") > -1)
                {
                    parentName = parentName.Substring(0, parentName.LastIndexOf("/"));
                }
                if (parentName.LastIndexOf("/") > -1)
                {
                    parentName = parentName.Substring(parentName.LastIndexOf("/") + 1);
                }

                // Given something like DVZ-1.3, we need to display "[DVZ-1.3] Divert zone"
                if (parentName.LastIndexOf("-") > -1)
                {
                    parentName = 
                        "[" + 
                        parentName.Substring(0, parentName.LastIndexOf("-")) + 
                        "-" + 
                        parentName.Substring(parentName.LastIndexOf("-") + 1) + 
                        "]";
                }
                return parentName;
            }
        }

        public string Type { get; set; }

        public string GrandParentName
        {
            get
            {
                string parentName = this.Id;

                if (parentName.LastIndexOf("/") > -1)
                {
                    parentName = parentName.Substring(0, parentName.LastIndexOf("/"));
                }
                if (parentName.LastIndexOf("/") > -1)
                {
                    parentName = parentName.Substring(0, parentName.LastIndexOf("/"));
                }
                if (parentName.LastIndexOf("/") > -1)
                {
                    parentName = parentName.Substring(parentName.LastIndexOf("/") + 1);
                }

                // Given something like DVZ-1.3, we need to display "[DVZ-1.3] Divert zone"
                if (parentName.LastIndexOf("-") > -1)
                {
                    parentName = CUtility.GetFullComponentNameDisplayString(
                        parentName.Substring(parentName.LastIndexOf("-") + 1),
                        parentName.Substring(0, parentName.LastIndexOf("-")));
                }

                return parentName;
            }
        }

        public string ShortId
        {
            get
            {
                string shortId = this.Id;

                if (shortId.LastIndexOf("/") > -1)
                {
                    shortId = shortId.Substring(shortId.LastIndexOf("/") + 1);
                }
                if (shortId.LastIndexOf(" ") > 0)
                {
                    shortId = shortId.Substring(1 + shortId.LastIndexOf(" "));
                }
                if (shortId.LastIndexOf(".") > 0)
                {
                    shortId = shortId.Substring(1 + shortId.LastIndexOf("."));
                }

                return shortId;
            }
        }
        
        public Dictionary<string, object> ToErrorDSRow(SectionTranslation sectionTranslation)
        {
            Dictionary<string, object> row = new Dictionary<string, object>();
            
            row.Add("Location", GrandParentName);
            row.Add("Section", ParentName);
            if(sectionTranslation.IsAvailable)
                row.Add("CustomSection", sectionTranslation.GetTranslation(ParentName));
            row.Add("Status", Status);
            row.Add("ElementName", DisplayName);
            row.Add("ElementShortId", ShortId);
            row.Add("Timestamp", ErrorTime);
            row.Add("ElementId", Id);
            row.Add("ProblemKey", ProblemKey);
            row.Add("SolutionKey", ProblemKey);
            row.Add("SortId", Id);
            row.Add("Updated", true);
            row.Add("RawAreaId", RawAreaId);
            row.Add("RawZoneId", RawZoneId);
            row.Add("RawSectionId", RawSectionId);
            row.Add("MarkCode", MarkCode);
            row.Add("ExtendedId", ExtendedId);
            row.Add("Type", Type);
            return row;
        }

        public static int CompareItemsById(CStructureItem left, CStructureItem right)
        {
            return left.Id.CompareTo(right.Id);
        }
    }
}
